package com.learn.springbootnetty.config;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class RS {

    private static final Logger logger = LoggerFactory.getLogger(RS.class);

    public static String success(Object returnObj) {
        JSONObject result = new JSONObject();
        result.put("success", true);
        result.put("data", returnObj);
        return result.toJSONString();
    }

    public static String error(String msg) {
        logger.error(msg);
        JSONObject result = new JSONObject();
        result.put("success", false);
        result.put("msg", msg);
        return result.toJSONString();
    }

    public static String error(String msg, String... formatter) {
        logger.error(msg, formatter);
        JSONObject result = new JSONObject();
        result.put("success", false);
        result.put("msg", msg);
        return result.toJSONString();
    }

    public static String error(String msg, Object data) {
        logger.error(msg);
        JSONObject result = new JSONObject();
        result.put("success", false);
        result.put("msg", msg);
        result.put("data", data);
        return result.toJSONString();
    }

    public static String exception(Exception e, String msg) {
        logger.error(e.getMessage(), e);
        JSONObject result = new JSONObject();
        result.put("success", false);
        result.put("msg", msg);
        result.put("e", e.getMessage());
        return result.toJSONString();
    }

    public static String exception(Exception e, String msg, JSONObject data) {
        logger.error(e.getMessage(), e);
        e.printStackTrace();
        JSONObject result = new JSONObject();
        result.put("success", false);
        result.put("msg", msg);
        result.put("data", data);
        result.put("e", e.getMessage());
        return result.toJSONString();
    }
}
